Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
feat: add dedupe option
Browse files Browse the repository at this point in the history
chore(package): update dependencies

chore(rollup): change require names

feat: add dedupe option

prevents bundling the same module multiple times by resolving it from the root node_modules

feat(rollup): add onwarn option to prevent this warnings

chore: change eslint settings for node files

style(node-test): fix lint warnings

style: fix lint warnings

test(rollup): warning messages

test: dedupe option
  • Loading branch information
next-kuscamara authored and kcmr committed Apr 14, 2020
1 parent a4b24a9 commit 873c6c4
Show file tree
Hide file tree
Showing 20 changed files with 5,305 additions and 3,338 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ module.exports = {
'blueprints/*/index.js',
'config/**/*.js',
'tests/dummy/config/**/*.js',
'node-tests/**/*.js'
'node-tests/**/*.js',
'lib/config/*.js'
],
excludedFiles: [
'addon/**',
'addon-test-support/**',
'app/**',
'tests/dummy/app/**'
'tests/dummy/app/**',
'tests/dummy/lib/**/node_modules/**'
],
parserOptions: {
sourceType: 'script',
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ import styles from './some-component.css';

The imported files exports the styles in a template literal that can be interpolated in the component's template. The processed styles are transformed with Autoprefixer using the hosting app targets configured in `config/targets.js`.

### `dedupe`

type: `Array`
default: `[]`

Module names that should be resolved from the app `node_modules` instead of another packages requiring them. This prevents bundling the same module multiple times.

Example:

```js
'ember-cli-webcomponents-bundler': {
modules: true,
entrypointPaths: [
'lib/path-one'
],
dedupe: ['lit-element', 'moment']
}
```

## Contribute

If you want to contribute to this addon, please read the [CONTRIBUTING.md](CONTRIBUTING.md).
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module.exports = {
modules: false,
entrypointPaths: [],
autoImport: true,
importStyles: false
importStyles: false,
dedupe: []
};

this.options = Object.assign(defaults, options);
Expand Down Expand Up @@ -132,6 +133,7 @@ module.exports = {
root: absEntrypointPath,
minify: this.options.minify,
outputfile: getOutputFileName(config),
dedupe: this.options.dedupe,
config
})
));
Expand Down
2 changes: 0 additions & 2 deletions lib/config/babel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-env node */

'use strict';

const common = {
Expand Down
2 changes: 0 additions & 2 deletions lib/config/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-env node */

'use strict';

const babelConfig = require('./babel');
Expand Down
25 changes: 18 additions & 7 deletions lib/config/rollup.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* eslint-env node */

'use strict';

const babel = require('rollup-plugin-babel');
const resolver = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const resolver = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const includePaths = require('rollup-plugin-includepaths');
const multiEntry = require('rollup-plugin-multi-entry');
const multiEntry = require('@rollup/plugin-multi-entry');
const terser = require('rollup-plugin-terser').terser;
const path = require('path');

Expand All @@ -15,6 +13,7 @@ module.exports = function({
outputfile,
root,
minify,
dedupe,
config
}) {
const legacy = config.name === 'legacy';
Expand All @@ -25,6 +24,14 @@ module.exports = function({
entrypoint
].filter(Boolean);

const onwarn = (warning, next) => {
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}

next(warning);
};

return {
rollup: {
input: inputFiles,
Expand All @@ -35,12 +42,16 @@ module.exports = function({
},
plugins: [
commonjs(),
resolver(),
resolver({
browser: true,
dedupe
}),
multiEntry({ exports: false }),
includePaths({ paths: [root] }),
babel(config.babel),
minify && terser()
]
],
onwarn
}
};
};
84 changes: 81 additions & 3 deletions node-tests/integration/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const MOCK_ENV_CONFIGS = {
default: path.resolve(__dirname, '../mocks/environment-default.js'),
modules: path.resolve(__dirname, '../mocks/environment-modules.js'),
noAutoImport: path.resolve(__dirname, '../mocks/environment-no-autoimport.js'),
noEntrypointPaths: path.resolve(__dirname, '../mocks/environment-no-entrypoints.js')
noEntrypointPaths: path.resolve(__dirname, '../mocks/environment-no-entrypoints.js'),
dedupe: path.resolve(__dirname, '../mocks/environment-dedupe.js'),
noDedupe: path.resolve(__dirname, '../mocks/environment-no-dedupe.js')
};

const emberCLIPath = path.resolve(__dirname, '../../node_modules/ember-cli/bin/ember');
Expand All @@ -24,11 +26,11 @@ function runEmberCommand(packagePath, command) {
return new Promise((resolve, reject) =>
exec(`${emberCLIPath} ${command}`, {
cwd: packagePath
}, (err, result) => {
}, (err, stdout, stderr) => {
if (err) {
reject(err);
}
resolve(result);
resolve({ stdout, stderr });
})
);
}
Expand Down Expand Up @@ -57,6 +59,7 @@ describe('ember-cli-webcomponents-bundler | Integration | options', function() {

before(async() => {
await mockConfig(mockConfigFile);

return runEmberCommand(fixturePath, 'build --prod');
});

Expand All @@ -81,6 +84,7 @@ describe('ember-cli-webcomponents-bundler | Integration | options', function() {

before(async() => {
await mockConfig(mockConfigFile);

return runEmberCommand(fixturePath, 'build --prod');
});

Expand All @@ -105,6 +109,7 @@ describe('ember-cli-webcomponents-bundler | Integration | options', function() {

before(async() => {
await mockConfig(mockConfigFile);

return runEmberCommand(fixturePath, 'build --prod');
});

Expand All @@ -129,6 +134,7 @@ describe('ember-cli-webcomponents-bundler | Integration | options', function() {

before(async() => {
await mockConfig(mockConfigFile);

return runEmberCommand(fixturePath, 'build --prod');
});

Expand All @@ -143,4 +149,76 @@ describe('ember-cli-webcomponents-bundler | Integration | options', function() {
assert.noFileContent(vendorPath, 'window.customElements.forcePolyfill');
});
});

context('without dedupe (default)', () => {
const mockConfigFile = MOCK_ENV_CONFIGS.noDedupe;

before(async() => {
await mockConfig(mockConfigFile);

return runEmberCommand(fixturePath, 'build --prod');
});

after(async() => {
restoreConfig(mockConfigFile);
await fs.remove(distPath);
});

it('nested modules are not deduped', () => {
const bundlePath = outputFilePath('assets/dedupe/bundle.js');

assert.fileContent(bundlePath, 'nested weightless module');
});
});

context('with dedupe', () => {
const mockConfigFile = MOCK_ENV_CONFIGS.dedupe;

before(async() => {
await mockConfig(mockConfigFile);

return runEmberCommand(fixturePath, 'build --prod');
});

after(async() => {
restoreConfig(mockConfigFile);
await fs.remove(distPath);
});

it('does not include multiple instances of deduped modules', () => {
const bundlePath = outputFilePath('assets/dedupe/bundle.js');

assert.noFileContent(bundlePath, 'nested weightless module');
});
});
});

describe('ember-cli-webcomponents-bundler | Integration | CLI', function() {
this.timeout(TEST_TIMEOUT);

context('warning messages', () => {
const mockConfigFile = MOCK_ENV_CONFIGS.modules;
let output = {};

before(async() => {
await mockConfig(mockConfigFile);

output = await runEmberCommand(fixturePath, 'build --prod');

return output;
});

after(async() => {
restoreConfig(mockConfigFile);
await fs.remove(distPath);
});

it('does not log warnings about rewriting `this`', () => {
assert.equal(output.stderr.includes('`this` has been rewritten to `undefined`'), false);
});

it('logs another warnings', () => {
assert.ok(output.stderr.includes('could not be resolved'));
});
});
});
27 changes: 27 additions & 0 deletions node-tests/mocks/environment-dedupe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const path = require('path');
const entrypoint = path.join('tests', 'dummy', 'lib');

module.exports = function(environment) {
const ENV = {
modulePrefix: 'dummy',
environment,
rootURL: '/',
locationType: 'auto',
EmberENV: {
EXTEND_PROTOTYPES: {
Date: false
}
},
'ember-cli-webcomponents-bundler': {
modules: false,
entrypointPaths: [
path.join(entrypoint, 'dedupe')
],
dedupe: ['weightless']
}
};

return ENV;
};
26 changes: 26 additions & 0 deletions node-tests/mocks/environment-no-dedupe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const path = require('path');
const entrypoint = path.join('tests', 'dummy', 'lib');

module.exports = function(environment) {
const ENV = {
modulePrefix: 'dummy',
environment,
rootURL: '/',
locationType: 'auto',
EmberENV: {
EXTEND_PROTOTYPES: {
Date: false
}
},
'ember-cli-webcomponents-bundler': {
modules: false,
entrypointPaths: [
path.join(entrypoint, 'dedupe')
]
}
};

return ENV;
};
Loading

0 comments on commit 873c6c4

Please sign in to comment.