我試圖讓彙總,commonjs,es6和樹木搖晃正常工作。彙總CommonJS,進口和出口與樹木搖擺
目前,我有以下構建腳本:
'use strict';
const rollup = require('rollup');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
rollup.rollup({
input: 'main.js',
format: 'iife',
plugins: [
{
transform(code, id) {
return code;
}
},
resolve({
extensions: ['.js', '.jsx']
}),
commonjs({
extensions: ['.js', '.jsx']
})
]
})
.then(({ generate }) => generate({
format: 'iife',
name: 'test',
}))
.then(({ code }) => console.log(code));
它加載以下main.js
文件
const { firstFunction } = require('./exports');
firstFunction();
和export.js
文件
export function firstFunction() {
return this.name;
}
export function secondFunction() {
return this.name;
}
輸出以下:
var test = (function() {
'use strict';
function firstFunction$1() {
return this.name;
}
function secondFunction() {
return this.name;
}
var exports$1 = Object.freeze({
firstFunction: firstFunction$1,
secondFunction: secondFunction
});
var require$$0 = (exports$1 && undefined) || exports$1;
const { firstFunction } = require$$0;
firstFunction();
var main = {
};
return main;
}());
我不能確定這是否是正確的行爲,我是假設,我將能夠使用樹搖動與ES6 export.js
文件,因此不需要導入從export.js
的secondFunction()
我們捆綁在一起的代碼。
我已經嘗試了一些設置的組合,但似乎沒有任何東西能夠讓樹形發揮作用。
這是值得注意的,我在服務器上使用commonjs並試圖使用客戶端上捆綁的相同文件 - 這就是爲什麼我有混合的cjs和es6。
使用導入而不是要求如果可能 – Lux
@Lux我需要使用commonjs for main.js,因爲它在節點上運行。 – Prisoner
如果你在'export.js'中使用'export'(假設缺少's'是一個錯字),這是沒有意義的。你也可以輸入'import'。彙總的整個想法是刪除所有的「輸入」,並用一個單一的IIFE(或任何你想要的)替換它們。 – Lux