Skip to content

Commit

Permalink
feat: support import attributes (#3849)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Oct 28, 2024
1 parent 735ec4d commit b998d3d
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 0 deletions.
11 changes: 11 additions & 0 deletions e2e/cases/import-attributes/basic/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { build, rspackOnlyTest } from '@e2e/helper';
import { expect } from '@playwright/test';

rspackOnlyTest('should support import attributes syntax', async () => {
const rsbuild = await build({
cwd: __dirname,
});

const indexJs = await rsbuild.getIndexFile();
expect(indexJs.content).toContain('with import attributes');
});
2 changes: 2 additions & 0 deletions e2e/cases/import-attributes/basic/loaderWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = (_source) =>
JSON.stringify({ type: 'with import attributes' });
16 changes: 16 additions & 0 deletions e2e/cases/import-attributes/basic/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
tools: {
rspack: {
module: {
rules: [
{
with: { type: 'json' },
loader: require.resolve('./loaderWith.js'),
},
],
},
},
},
});
3 changes: 3 additions & 0 deletions e2e/cases/import-attributes/basic/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import one from './pkg-1.json' with { type: 'json' };

console.log('one', one);
3 changes: 3 additions & 0 deletions e2e/cases/import-attributes/basic/src/pkg-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "none"
}
4 changes: 4 additions & 0 deletions packages/core/src/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ function getDefaultSwcConfig(
},
experimental: {
cacheRoot,
/**
* Preserve `with` in imports and exports.
*/
keepImportAttributes: true,
},
},
isModule: 'unknown',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -142,6 +143,7 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -142,6 +143,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -495,6 +497,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -538,6 +541,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -914,6 +918,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -953,6 +958,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1265,6 +1271,7 @@ exports[`tools.rspack > should match snapshot 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1308,6 +1315,7 @@ exports[`tools.rspack > should match snapshot 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,7 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1561,6 +1562,7 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1847,6 +1849,7 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1886,6 +1889,7 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down
26 changes: 26 additions & 0 deletions packages/core/tests/__snapshots__/swc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ exports[`plugin-swc > should add browserslist 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -78,6 +79,7 @@ exports[`plugin-swc > should add browserslist 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -144,6 +146,7 @@ exports[`plugin-swc > should add pluginImport 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -194,6 +197,7 @@ exports[`plugin-swc > should add pluginImport 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -310,6 +314,7 @@ exports[`plugin-swc > should allow to use \`tools.swc\` to configure swc-loader
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": false,
"parser": {
Expand Down Expand Up @@ -353,6 +358,7 @@ exports[`plugin-swc > should allow to use \`tools.swc\` to configure swc-loader
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": false,
"parser": {
Expand Down Expand Up @@ -415,6 +421,7 @@ exports[`plugin-swc > should apply environment config correctly 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -470,6 +477,7 @@ exports[`plugin-swc > should apply environment config correctly 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -528,6 +536,7 @@ exports[`plugin-swc > should apply environment config correctly 2`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -574,6 +583,7 @@ exports[`plugin-swc > should apply environment config correctly 2`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -641,6 +651,7 @@ exports[`plugin-swc > should apply pluginImport correctly when ConfigChain 1`] =
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -694,6 +705,7 @@ exports[`plugin-swc > should apply pluginImport correctly when ConfigChain 1`] =
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -770,6 +782,7 @@ exports[`plugin-swc > should disable pluginImport when return undefined 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -813,6 +826,7 @@ exports[`plugin-swc > should disable pluginImport when return undefined 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -875,6 +889,7 @@ exports[`plugin-swc > should disable preset_env in target other than web 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -914,6 +929,7 @@ exports[`plugin-swc > should disable preset_env in target other than web 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -980,6 +996,7 @@ exports[`plugin-swc > should disable preset_env mode 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1023,6 +1040,7 @@ exports[`plugin-swc > should disable preset_env mode 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1096,6 +1114,7 @@ exports[`plugin-swc > should enable entry mode preset_env 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1143,6 +1162,7 @@ exports[`plugin-swc > should enable entry mode preset_env 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1216,6 +1236,7 @@ exports[`plugin-swc > should enable usage mode preset_env 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1264,6 +1285,7 @@ exports[`plugin-swc > should enable usage mode preset_env 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1337,6 +1359,7 @@ exports[`plugin-swc > should has correct core-js 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1384,6 +1407,7 @@ exports[`plugin-swc > should has correct core-js 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1446,6 +1470,7 @@ exports[`plugin-swc > should has correct core-js 2`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -1485,6 +1510,7 @@ exports[`plugin-swc > should has correct core-js 2`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-babel/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ exports[`plugins/babel > babel-loader should works with builtin:swc-loader 1`] =
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/plugin-babel/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -117,6 +118,7 @@ exports[`plugins/babel > should apply environment config correctly 1`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/plugin-babel/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down Expand Up @@ -194,6 +196,7 @@ exports[`plugins/babel > should apply environment config correctly 2`] = `
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/plugin-babel/tests/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"parser": {
Expand Down
Loading

0 comments on commit b998d3d

Please sign in to comment.