Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use swc in dev & prod #321

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8393f4e
try out swc
JoviDeCroock Jan 31, 2021
8f08757
try minify
JoviDeCroock Jan 31, 2021
be42053
minify
JoviDeCroock Jan 31, 2021
82c6b27
reduce changes
JoviDeCroock Jan 31, 2021
5e0c361
try in nomodule
JoviDeCroock Jan 31, 2021
ab730fb
undo changes in main app
JoviDeCroock Jan 31, 2021
acd10fb
add transpile step
JoviDeCroock Feb 5, 2021
3dc2199
try to walk through every file
JoviDeCroock Feb 5, 2021
f72798f
convert to swc-plugin
JoviDeCroock Feb 6, 2021
5b543e6
remove await
JoviDeCroock Feb 6, 2021
204a3a6
make swc work
JoviDeCroock Feb 11, 2021
abc7c9a
target granularly
JoviDeCroock Feb 11, 2021
3fc2625
cleanup
JoviDeCroock Feb 11, 2021
32b43f4
type it correctly
JoviDeCroock Feb 11, 2021
e250962
consolidate plugins
JoviDeCroock Feb 11, 2021
c883de0
consolidate branches
JoviDeCroock Feb 11, 2021
6bc2e33
skip transformations
JoviDeCroock Feb 11, 2021
fe01987
be more lean in jsxImportAdder
JoviDeCroock Feb 11, 2021
d51450f
revert
JoviDeCroock Feb 11, 2021
a63d2a4
try the htm-plugin
JoviDeCroock Feb 11, 2021
d0cb359
try the htm-plugin
JoviDeCroock Feb 11, 2021
4e0563d
fix issue with tests
JoviDeCroock Feb 12, 2021
e07fffe
temp patch to see if tests succeed
JoviDeCroock Feb 12, 2021
a45bb6a
fix boot test
JoviDeCroock Feb 12, 2021
0e053fe
parameterize imports
JoviDeCroock Feb 13, 2021
9c0518b
lint
JoviDeCroock Feb 13, 2021
274d555
cleanup rebase
JoviDeCroock Feb 18, 2021
72dcf01
apply formatting and remove log
JoviDeCroock Feb 18, 2021
705e528
add changeset
JoviDeCroock Feb 18, 2021
6dd5736
remove patches in wait for swc bump
JoviDeCroock Feb 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-cars-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': minor
---

Use swc to transpile our dfiles
1 change: 1 addition & 0 deletions packages/nomodule-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ function downlevel(code, fileName) {
}
]
});

return result.code;
}
2 changes: 1 addition & 1 deletion packages/wmr/demo/public/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, render } from 'preact';
import { render } from 'preact';
import { LocationProvider, Router } from './lib/loc.js';
import lazy, { ErrorBoundary } from './lib/lazy.js';
import Home from './pages/home.js';
Expand Down
4 changes: 3 additions & 1 deletion packages/wmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
"jest": {
"preset": "jest-puppeteer"
},
"dependencies": {
"@swc/core": "^1.2.49"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
Expand Down Expand Up @@ -144,7 +147,6 @@
"semver": "^7.3.2",
"sirv": "^1.0.6",
"stylis": "^4.0.2",
"sucrase": "^3.15.0",
"tar-stream": "^2.1.3",
"terser": "^4.8.0",
"tmp-promise": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/wmr/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const config = {
}
]
},
external: [...builtins],
external: [...builtins, '@swc/core'],
// /* Logs all included npm dependencies: */
// external(source, importer) {
// const ch = source[0];
Expand Down
14 changes: 5 additions & 9 deletions packages/wmr/src/bundler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { relative, sep, posix, resolve, dirname } from 'path';
import * as rollup from 'rollup';
import htmPlugin from './plugins/htm-plugin.js';
import sucrasePlugin from './plugins/sucrase-plugin.js';
import wmrPlugin from './plugins/wmr/plugin.js';
import wmrStylesPlugin from './plugins/wmr/styles-plugin.js';
import sassPlugin from './plugins/sass-plugin.js';
Expand All @@ -23,6 +21,7 @@ import externalUrlsPlugin from './plugins/external-urls-plugin.js';
import copyAssetsPlugin from './plugins/copy-assets-plugin.js';
import nodeBuiltinsPlugin from './plugins/node-builtins-plugin.js';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import swcPlugin from './plugins/swc-plugin.js';

/** @param {string} p */
const pathToPosix = p => p.split(sep).join(posix.sep);
Expand All @@ -36,6 +35,7 @@ const pathToPosix = p => p.split(sep).join(posix.sep);
* @property {string} [root = ''] cwd without implicit ./public dir
* @property {string} [publicDir = '']
* @property {string} [publicPath = '/']
* @property {object} [features]
* @property {string} [out = '.cache']
* @property {boolean} [sourcemap]
* @property {boolean} [minify = true]
Expand Down Expand Up @@ -72,7 +72,8 @@ export async function bundleProd({
plugins,
output,
minify = true,
npmChunks = false
npmChunks = false,
features
}) {
cwd = cwd || '';
root = root || cwd;
Expand All @@ -95,19 +96,14 @@ export async function bundleProd({
plugins: plugins.concat([
nodeBuiltinsPlugin({ production: true }),
externalUrlsPlugin(),
sucrasePlugin({
typescript: true,
sourcemap,
production: true
}),
swcPlugin({ jsx: features.jsx, from: features.from }),
htmlEntriesPlugin({ cwd, publicDir, publicPath }),
(dynamicImportVars.default || dynamicImportVars)({
include: /\.(m?jsx?|tsx?)$/,
exclude: /\/node_modules\//
}),
publicPathPlugin({ publicPath }),
aliasesPlugin({ aliases, cwd: root }),
htmPlugin({ production: true }),
sassPlugin({ production: true }),
wmrStylesPlugin({ hot: false, cwd }),
wmrPlugin({ hot: false }),
Expand Down
14 changes: 3 additions & 11 deletions packages/wmr/src/lib/compile-single-module.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as rollup from 'rollup';
import wmrPlugin from '../plugins/wmr/plugin.js';
import htmPlugin from '../plugins/htm-plugin.js';
import sucrasePlugin from '../plugins/sucrase-plugin.js';
import swcPlugin from '../plugins/swc-plugin.js';
// import localNpmPlugin from './plugins/local-npm-plugin.js';

// disabled for now
Expand Down Expand Up @@ -47,15 +46,8 @@ export const compileSingleModule = withCache(async (input, { cwd, out }) => {
return { id, external: true, moduleSideEffects: true };
}
},
sucrasePlugin({
typescript: true,
sourcemap: false,
production: false
}),
// localNpmPlugin(),
// wmrStylesPlugin({ cwd }),
wmrPlugin(),
htmPlugin()
swcPlugin(),
wmrPlugin()
]
});
cache = bundle.cache;
Expand Down
6 changes: 5 additions & 1 deletion packages/wmr/src/lib/normalize-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export async function normalizeOptions(options, mode) {
options.plugins = [];
options.output = [];
options.middleware = [];
options.features = { preact: true };
options.features = {
preact: true,
jsx: 'h',
from: 'preact'
};

// `wmr` / `wmr start` is a development command.
// `wmr build` / `wmr serve` are production commands.
Expand Down
1 change: 1 addition & 0 deletions packages/wmr/src/plugins/fast-minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function fastMinifyPlugin({ sourcemap = false, warnThreshold = 50
name: 'fast-minify',
renderChunk(code, chunk) {
const start = Date.now();

const out = terser.minify(code, {
sourceMap: sourcemap,
mangle: true,
Expand Down
63 changes: 0 additions & 63 deletions packages/wmr/src/plugins/sucrase-plugin.js

This file was deleted.

180 changes: 180 additions & 0 deletions packages/wmr/src/plugins/swc-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import Visitor from '@swc/core/Visitor.js';
import swc from '@swc/core';

class JSXImportAppender extends Visitor.default {
constructor(options) {
super();
this.hasJSX = false;
this.jsx = options.jsx;
this.from = options.from;
}

visitTsType(e) {
return e;
}

visitJSXOpeningElement(e) {
this.hasJSX = true;
return super.visitJSXOpeningElement(e);
}

visitModule(e) {
super.visitModule(e);

if (!this.hasJSX) return e;

const imports = e.body.filter(d => d.type === 'ImportDeclaration');
const preactImport = imports.find(imp => imp.source.value === this.from);

if (!preactImport) {
e.body.unshift({
type: 'ImportDeclaration',
span: { start: 0, end: 0, ctxt: 0 },
specifiers: [
{
type: 'ImportSpecifier',
span: { start: 0, end: 0, ctxt: 0 },
local: {
type: 'Identifier',
span: { start: 0, end: 0, ctxt: 0 },
value: this.jsx,
typeAnnotation: null,
optional: false
}
},
{
type: 'ImportSpecifier',
span: { start: 0, end: 0, ctxt: 0 },
local: {
type: 'Identifier',
span: { start: 0, end: 0, ctxt: 0 },
value: 'Fragment',
typeAnnotation: null,
optional: false
}
}
],
source: {
type: 'StringLiteral',
span: { start: 0, end: 0, ctxt: 0 },
value: this.from,
hasEscape: false,
kind: { type: 'normal', containsQuote: true }
},
asserts: null,
typeOnly: false
});

return { ...e, body: [...e.body] };
}

const hasH = imports.find(imp => !!imp.specifiers.find(x => x.local.value === this.jsx));
if (!hasH) {
preactImport.specifiers.push({
type: 'ImportSpecifier',
span: { start: 0, end: 0, ctxt: 0 },
typeAnnotation: null,
optional: false,
local: {
type: 'Identifier',
span: { start: 0, end: 0, ctxt: 0 },
value: this.jsx,
typeAnnotation: null,
optional: false
}
});
}

const hasFrag = imports.find(imp => !!imp.specifiers.find(x => x.local.value === 'Fragment'));
if (!hasFrag) {
preactImport.specifiers.push({
type: 'ImportSpecifier',
span: { start: 0, end: 0, ctxt: 0 },
typeAnnotation: null,
optional: false,
local: {
type: 'Identifier',
span: { start: 0, end: 0, ctxt: 0 },
value: 'Fragment',
typeAnnotation: null,
optional: false
}
});
}

return { ...e, body: [...e.body] };
}
}

/**
* Transform JS/TS files with swc.
* @param {object} [opts]
* @param {string} [opts.jsx]
* @param {string} [opts.from]
* @returns {import('rollup').Plugin}
*/
const swcPlugin = ({ jsx = 'h', from = 'preact' }) => ({
name: 'swc',
async transform(code, filename) {
if (/^[\0\b]/.test(filename) || !/\.(mjs|jsx?|tsx?)$/.test(filename)) return null;

let result = { code };
if (/tsx?$/.test(filename)) {
result = await swc.transform(result.code, {
test: '.*.tsx?$',
jsc: {
loose: true,
transform: {
react: {
pragma: jsx,
pragmaFrag: 'Fragment',
development: false,
throwIfNamespace: false,
useBuiltins: false
}
},
parser: {
syntax: 'typescript',
tsx: true,
dynamicImport: true
},
target: 'es2018'
},
filename,
plugin: m => {
return new JSXImportAppender({ jsx, from }).visitModule(m);
}
});
} else {
result = await swc.transform(result.code, {
test: '.*.jsx?$',
jsc: {
loose: true,
transform: {
react: {
pragma: jsx,
pragmaFrag: 'Fragment',
development: false,
throwIfNamespace: false,
useBuiltins: false
}
},
parser: {
syntax: 'ecmascript',
jsx: true,
dynamicImport: true
},
target: 'es2018'
},
filename,
plugin: m => {
return new JSXImportAppender({ jsx, from }).visitModule(m);
}
});
}

return result;
}
});

export default swcPlugin;